from flask import Flask, render_template, request, session, redirect, url_for
import pandas as pd

app = Flask("学生成绩管理")
app.config["SECRET_KEY"] = 'sdafasfsafsafd'


@app.route("/")
def index():
    return render_template("index.html")


@app.route("/login", methods=["POST", "GET"])
def login():
    # alt enter
    if request.method == "POST":
        # ctrl d
        user_name = request.form.get("user_name")
        password = request.form.get("password")
        if user_name == "admin" and password == "admin666":
            session["user_name"] = "admin"
            return redirect(url_for("index"))

    return render_template("login.html")


@app.route("/logout")
def logout():
    session.pop("user_name")
    return redirect(url_for("index"))


@app.route("/show_grade")
def show_grade():
    if "user_name" not in session:
        return redirect(url_for("index"))

    df = pd.read_excel("学生成绩表.xlsx")
    return render_template("show_grade.html",
                           table_html=df.to_html(classes="table table-hover table-sm"))

@app.route("/submit_grade", methods=["POST", "GET"])
def submit_grade():
    if "user_name" not in session:
        return redirect(url_for("index"))

    if request.method == "POST":
        name = request.form.get("name")
        yuwen = request.form.get("yuwen")
        shuxue = request.form.get("shuxue")
        yingyu = request.form.get("yingyu")
        df = pd.read_excel("学生成绩表.xlsx")
        # 姓名	语文成绩	数学成绩	英语成绩
        new_row = pd.DataFrame({
            "姓名" : [name],
            "语文成绩" : [yuwen],
            "数学成绩" : [shuxue],
            "英语成绩" : [yingyu],
        })
        df = pd.concat([new_row, df])
        df.to_excel("学生成绩表.xlsx", index=False)
        return redirect(url_for("show_grade"))
    return render_template("submit_grade.html")

app.run(debug=True)
